// packageA/pages/operate/index/index.js
const App = getApp()
import moment from '../../../../utils/moment'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    dayshow: false,
    currentIndex:0,
    start:'',
    end:'',
    topData:{},
    action:'all_demo_list',
    deviceList: [],    //列表
    deviceTotal: 0,    //总数
    devicePages: 0,     //总页数
    deviceIndex: 1,      //当前页 
    searchLoading: false, //加载更多
    deviceNoData: false,  //暂无更多
    searchLoadingComplete: false, //加载到底部
    tabs: [
      { key: 'today_clue', title: '录入' },
      { key: 'time_out', title: '开户' },
      { key: 'inspect', title: '待续费' }
    ],
    actions: [
      {
        name: '今日',
      },
      {
        name: '昨日',
      },
      {
        name: '本周',
      },
      {
        name: '上月',
      },
    ],
    text:'今日',
    datetime:''
  },
//日历
  onDisplay() {
    this.setData({ show: true });
    console.log(this.selectComponent('#calendar'))
    const cal = this.selectComponent('#calendar')
    cal.setData({
      initFinished:false,
      showCalendar:true
    })
  },
  onClose() {
    this.setData({ show: false });
    this.setData({ dayshow: false });
  },
//今日选择
onSelect(event) {
  console.log(event.detail);
  this.setData({
    text:event.detail.name
  })
},
dayshowbtn(){
  this.setData({ dayshow: true });
},
detaillist(){
  wx.navigateTo({
    url: '/packageA/pages/operate/demonstration/demonstration-list',
  })
},
  /**
   * tab切换
   */
  handleTab(e) {
    let { index, value } = e.currentTarget.dataset
    let vbal
    if(index == 0){
      vbal = 'all_demo_list'
    }else if(index == 1){
      vbal = 'all_open_list'
    }else{
      vbal = 'need_fee_list'
    }
    this.setData({
      currentIndex: index,
      action:vbal
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onDayClick(event){
    this.setData({ 
      show: false,
      start:moment(event.detail.begin).format('YYYY-MM-DD'),
      end:moment(event.detail.end).format('YYYY-MM-DD'),
      datetime:`${moment(event.detail.begin).format('YYYY-MM-DD')}-${moment(event.detail.end).format('YYYY-MM-DD')}`
     });
     this.getHomeData()
  },
  // 列表跳转
  handleToList(){
    wx.navigateTo({
      url: '../index-list/index',
    })
  },
//
getHomeData(){
  let _this = this
  App._post_form('Operation/getBoosHomeData', {
    start_date:_this.data.start,
    end_date:_this.data.end
  }, function (result) {
    console.log(result);
    _this.setData({
      topData: result.data
    })
  })
},
getList() {
  let _this = this
  wx.showLoading({
    title: '加载中...',
  })
  // console.log(_this.data.deviceIndex);
  App._post_form('Operation/getBusinessList', {
    action:_this.data.action,
    page: _this.data.deviceIndex,
  }, function (result) {
    console.log(result)
    wx.hideLoading()
    let Result = result.data.data

    // 响应无数据 则提示无数据
    if (_this.data.deviceIndex == 1 && Result.length == 0) {
      console.log('aaaaa')
      _this.setData({
        deviceNoData: true,
        searchLoading: false,
        deviceList:[],
        searchLoadingComplete: false
      })
    }else{
      _this.setData({
        deviceList: Result, //获取数据数组
        deviceNoData: false //“没有数据”隐藏
      });
    }
    // if (Result.length) {
    //   _this.data.deviceTotal = result.data.count
    //   _this.data.devicePages = result.data.total_page
    //   let deviceListTemp = [];
    //   if (_this.data.deviceIndex > 1) {
    //     deviceListTemp = _this.data.deviceList.concat(Result);
    //     var result = [];
    //     var obj = {};
    //     for (var i = 0; i < deviceListTemp.length; i++) {
    //       if (!obj[deviceListTemp[i].id]) {
    //         result.push(deviceListTemp[i]);
    //         obj[deviceListTemp[i].id] = true;
    //       }
    //     }
    //     console.log(deviceListTemp)
    //     _this.setData({
    //       searchLoading: true, //"正在载入更多"显示
    //     });
    //   } else {
    //     deviceListTemp = Result;
    //   }
    //   _this.setData({
    //     deviceList: deviceListTemp, //获取数据数组
    //     deviceNoData: false //“没有数据”隐藏
    //   });
    // }
    console.log(Result)
    
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHomeData()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getHomeData()
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})